package com.loongtech.bi.manager.system;

import com.loongtech.bi.entity.POJO.Page;
import com.loongtech.bi.entity.system.EntityAdvConfig;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Utils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.loongtech.bi.manager.system.SysProjectManager.projectIdNameMap;

@Service
public class AdvConfigManager extends ManagerQueryCacheBase<EntityAdvConfig>  {
	private static final long serialVersionUID = 2L;
	
	public static List<EntityAdvConfig> ADV_CONFIG_LIST;  // 所有
    public static Set<String> AD_LIST;  // 所有代理商名称
    public static Set<String> ID_LIST;  // 所有代理商名称

	public void init() {
		ADV_CONFIG_LIST = findByQuery("from EntityAdvConfig order by id");

        AD_LIST = new HashSet<>();
        ID_LIST = new HashSet<>();
        for (EntityAdvConfig e : ADV_CONFIG_LIST) {
            AD_LIST.add(e.getAgent());
            ID_LIST.add(e.getId());
        }
	}

    /**
     * 查询任务(springmvc)
     */
    public Page<List<EntityAdvConfig>> queryByCriteria(Integer curPage, Integer pageSize, EntityAdvConfig entity) {
        List<Object> cons = new ArrayList<>();
        StringBuilder hql = new StringBuilder(" from EntityAdvConfig l where 1=1 ");
        if (!Utils.isEmptyStr(entity.getId())) {
            hql.append(" and l.id = ? ");
            cons.add(entity.getId());
        }
        if (!Utils.isEmptyStr(entity.getAffiliation())) {
            hql.append(" and l.affiliation = ? ");
            cons.add(entity.getAffiliation());
        }
        if (!Utils.isEmptyStr(entity.getActName())) {
            hql.append(" and l.actName = ? ");
            cons.add(entity.getActName());
        }
        if (entity.getGameId() != null) {
            hql.append(" and l.gameId = ?");
            cons.add(entity.getGameId());
        }
        if (!Utils.isEmptyStr(entity.getChannelName())) {
            hql.append(" and l.channelName = ? ");
            cons.add(entity.getChannelName());
        }
        if (!Utils.isEmptyStr(entity.getAgent())) {
            hql.append(" and l.agent = ? ");
            cons.add(entity.getAgent());
        }
        if (!Utils.isEmptyStr(entity.getPlatform())) {
            hql.append(" and l.platform = ? ");
            cons.add(entity.getPlatform());
        }
        if (!Utils.isEmptyStr(entity.getPaytype())) {
            hql.append(" and l.paytype = ? ");
            cons.add(entity.getPaytype());
        }
        if (!Utils.isEmptyStr(entity.getAdvPosition())) {
            hql.append(" and l.advPosition = ? ");
            cons.add(entity.getAdvPosition());
        }
        Integer totalSize = countNum(hql.toString(), cons.toArray());
        //数据
        List<EntityAdvConfig> list = findByQuery((curPage - 1) * pageSize, pageSize, hql.toString(), cons.toArray());
        if (list != null && list.size() > 0) {
            for (EntityAdvConfig entityAdv : list) {
                entityAdv.setGameName(projectIdNameMap.get(entityAdv.getGameId().toString()));
            }
        }
        return new Page<>(list, totalSize, curPage, pageSize);
    }




}
